package com.jspx.txweb.result;

import com.jspx.txweb.util.TXWebUtil;
import com.jspx.boot.environment.Environment;
import com.jspx.json.JSONException;
import com.jspx.json.JSONObject;
import com.jspx.json.XML;
import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.dispatcher.Dispatcher;
import com.jspx.utils.XMLUtil;

/**
 * Created by chenyuan on 2015-7-10.
 * 这里只是一个适配器，真是执行是 getRocAutoResult  方法
 *
*/

public class WebServiceResult extends ResultSupport {
    private JSONObject json = new JSONObject();
    private boolean outXML = false;
    public String getResult()
    {
        //如果头部设置为javascript mootools IE下会自动执行
        if (outXML) {
            try {
                return "<?xml version=\"1.0\" encoding=\"" + Dispatcher.getEncode() + "\"?>\r\n" + (debug?XMLUtil.format(XML.toString(json, Environment.rocResult)):XML.toString(json, Environment.rocResult));
            } catch (JSONException e) {
                return TXWebUtil.createRocErrorInfo("",-32700, " xml 格式解析异常" ).toString(4);
            }
        } else
        {
            return debug?json.toString(4):json.toString();
        }
    }

    public void execute(ActionInvocation actionInvocation) throws JSONException {
        json = getRocAutoResult(actionInvocation);
        JSONObject callJson = actionInvocation.getActionProxy().getCallJson();
        if (callJson != null && "xml".equalsIgnoreCase(callJson.getString(Environment.rocFormat)))
        {
            outXML = true;
        }
    }
}